﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.IO.Ports;
using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;
using MasterDatabase;

namespace Job_Assignment
{
    public partial class Form1 : SQL_APPL
    {
        public DataTable PlanLine_DatePlan_Tbl = new DataTable();
        public DataSet PlanLine_DatePlan_ds = new DataSet();
        public SqlDataAdapter PlanLine_DatePlan_da;

        void PlanForLine_Create_BT_Click(object sender, EventArgs e)
        {
            bool b;
            string sql_cmd;
            DataTable list_wst;
            string line_id, line_name, shift, wst, wst_name, part;
            DateTime date;
            TimeSpan from_time, to_time;
            int count;
            string mess;
            DataTable mainpart_tbl;

            // Hien thi chon ngay
            DateSelect_Dialog_Form selectDate_Dialog = new DateSelect_Dialog_Form(DateTime.Now);
            if (selectDate_Dialog.ShowDialog() == DialogResult.OK)
            {
                date = selectDate_Dialog.Select_Date;
                // lay du lieu ke hoach sx theo line cua ngay da chon
                sql_cmd = String.Format("SELECT * FROM [JOB_ASSIGNMENT_DB].[dbo].[P_003_KeHoachSanXuatTheoLine] WHERE [Date] = '{0}' order by LineId", date.ToString("yyyy-MMM-dd"));
                b = P_003_KeHoachSanXuatTheoLine_MasterDatabase.MasterDatabase_GridviewTBL.Load_DataBase(MasterDatabase_Connection_Str, sql_cmd);
                if (b == false)
                {
                    return;
                }

                count = P_003_KeHoachSanXuatTheoLine_MasterDatabase.MasterDatabase_GridviewTBL.Data_dtb.Rows.Count;
                if (count > 0)
                {
                    mess = "Plan for date:" + date.ToString("dd MMM yyyy") + "was existing\n";
                    mess += "Do you want to delete and create the new one?";

                    if (MessageBox.Show(mess, "Warning", MessageBoxButtons.YesNo) == DialogResult.No)
                    {
                        return;
                    }
                    DeletePlanforLine(date);
                    P_003_KeHoachSanXuatTheoLine_MasterDatabase.MasterDatabase_GridviewTBL.Load_DataBase(MasterDatabase_Connection_Str, sql_cmd);
                }

                sql_cmd = String.Format("SELECT DISTINCT [LineID] FROM [JOB_ASSIGNMENT_DB].[dbo].[P_002_PlanForProductionByDate] WHERE [Date] = '{0}' order by LineId", date.ToString("yyyy-MMM-dd"));

                // Lay du lieu Ke hoach cua ngay da chon
                // Kiem tra co bao nhieu line duoc san xuat trong ngay
                if (PlanLine_DatePlan_Tbl != null)
                {
                    PlanLine_DatePlan_Tbl.Clear();
                }
                PlanLine_DatePlan_Tbl = Get_SQL_Data(MasterDatabase_Connection_Str, sql_cmd, ref PlanLine_DatePlan_da, ref PlanLine_DatePlan_ds);

                foreach (DataRow line_plan_row in PlanLine_DatePlan_Tbl.Rows)
                {
                    // for Shift_1
                    shift = SHIFT_1;
                    line_id = line_plan_row["LineID"].ToString().Trim();
                    mainpart_tbl = Get_Main_Part_Shift_1(date, line_id).Copy();
                    if ((mainpart_tbl != null) && (mainpart_tbl.Rows.Count > 0))
                    {
                        part = mainpart_tbl.Rows[0]["PartNumber"].ToString().Trim();
                        line_name = mainpart_tbl.Rows[0]["LineName"].ToString().Trim();
                        from_time = TimeSpan.Parse("6:00:00");
                        to_time = Get_OutTime_Shift_1(date, line_id);

                        list_wst = Get_List_wst(part);
                        foreach (DataRow wst_row in list_wst.Rows)
                        {
                            //Create new data for Plan of Line
                            wst = wst_row["WST_ID"].ToString();
                            wst_name = wst_row["WST_Name"].ToString();
                            Create_Update_PlanLine(date, line_id, line_name, shift, part, wst, wst_name, from_time, to_time);
                        }
                    }

                    // for Shift_2
                    shift = SHIFT_2;
                    line_id = line_plan_row["LineID"].ToString().Trim();
                    mainpart_tbl = Get_Main_Part_Shift_2(date, line_id).Copy();
                    if ((mainpart_tbl != null) && (mainpart_tbl.Rows.Count > 0))
                    {
                        part = mainpart_tbl.Rows[0]["PartNumber"].ToString().Trim();
                        line_name = mainpart_tbl.Rows[0]["LineName"].ToString().Trim();
                        from_time = TimeSpan.Parse("6:00:00");
                        to_time = TimeSpan.Parse("14:00:00");

                        list_wst = Get_List_wst(part);
                        foreach (DataRow wst_row in list_wst.Rows)
                        {
                            //Create new data for Plan of Line
                            wst = wst_row["WST_ID"].ToString();
                            wst_name = wst_row["WST_Name"].ToString();
                            Create_Update_PlanLine(date, line_id, line_name, shift, part, wst, wst_name, from_time, to_time);
                        }
                    }

                    // for Shift_3
                    shift = SHIFT_3;
                    line_id = line_plan_row["LineID"].ToString().Trim();
                    mainpart_tbl = Get_Main_Part_Shift_3(date, line_id).Copy();
                    if ((mainpart_tbl != null) && (mainpart_tbl.Rows.Count > 0))
                    {
                        part = mainpart_tbl.Rows[0]["PartNumber"].ToString().Trim();
                        line_name = mainpart_tbl.Rows[0]["LineName"].ToString().Trim();
                        from_time = Get_InTime_Shift_3(date, line_id);
                        to_time = TimeSpan.Parse("6:00:00");

                        list_wst = Get_List_wst(part);
                        foreach (DataRow wst_row in list_wst.Rows)
                        {
                            //Create new data for Plan of Line
                            wst = wst_row["WST_ID"].ToString();
                            wst_name = wst_row["WST_Name"].ToString();
                            Create_Update_PlanLine(date, line_id, line_name, shift, part, wst, wst_name, from_time, to_time);
                        }
                    }
                }
            }
        }
        //<<Long_15_06
        private DataTable CreateListEmployee(int index)
        {
            //tam thoi tao table tam
            DataTable tb = new DataTable();
            tb.Columns.Add("Empl_ID", typeof(string));
            tb.Columns.Add("Empl_Name", typeof(string));

            tb.Rows.Add(new object[] { "Empl_ID " + index, "Empl_Name " + index });
            tb.Rows.Add(new object[] { "Empl_ID " + (index + 1), "Empl_Name " + (index + 1)});
            tb.Rows.Add(new object[] { "Empl_ID " + (index + 2), "Empl_Name " + (index +2)});

            return tb;
        }

        private void ApplyListEmployeeIntoGridView()
        {
            for (int i = 0; i < P_003_KeHoachSanXuatTheoLine_MasterDatabase.MasterDatabase_GridviewTBL.GridView.Rows.Count; i++)
            {
                DataGridViewMultiColumnComboBoxCell cell = P_003_KeHoachSanXuatTheoLine_MasterDatabase.MasterDatabase_GridviewTBL.GridView["Empl_ID", i] as DataGridViewMultiColumnComboBoxCell;
                cell.DataSource = CreateListEmployee(i);
            }
        }
        //>>Long_15_06
        private DataTable GetSqlData(string sqlCmd)
        {
            DataTable temp_dtb = new DataTable();
            DataSet inputData_tbl = new DataSet();
            SqlDataAdapter addapter = new SqlDataAdapter();

            if (temp_dtb != null)
            {
                temp_dtb.Clear();
            }
            temp_dtb = Get_SQL_Data(MasterDatabase_Connection_Str, sqlCmd, ref addapter, ref inputData_tbl);

            return temp_dtb;
        }

        void PlanForLine_Empl_Asign_BT_Click(object sender, EventArgs e)
        {

            string sql_cmd = @"SELECT distinct [WorkStationID],[LineID],[SkillID]
                                FROM [JOB_ASSIGNMENT_DB].[dbo].[MDB_004_LineSkillRequest] ";

            string sql_cmd_1 = @"SELECT distinct [Skill_ID],[Empl_ID],[Empl_Name],[Priority]
                                FROM [JOB_ASSIGNMENT_DB].[dbo].[MDB_002_Empl_Skill] ";

            string sql_cmd_2 = @"SELECT * FROM [JOB_ASSIGNMENT_DB].[dbo].[P_003_KeHoachSanXuatTheoLine] ";

            DataTable PlanLine_WST_Plan = P_003_KeHoachSanXuatTheoLine_MasterDatabase.MasterDatabase_GridviewTBL.Data_dtb;

            if ((PlanLine_WST_Plan == null) || (PlanLine_WST_Plan.Rows.Count == 0))
            {
                MessageBox.Show("No task to arrange for Employee", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            DataTable wst_line_skill_tbl = GetSqlData(sql_cmd);
            DataTable empl_vs_skill_tbl = GetSqlData(sql_cmd_1);
            //DataTable PlanLine_WST_Plan = Get_PlanByWST();

            //Find the skill for each workstation
            EmployeeAssignment engine = new EmployeeAssignment(PlanLine_WST_Plan, empl_vs_skill_tbl, wst_line_skill_tbl);
            PlanLine_WST_Plan = engine.GetFinalList();


            ApplyListEmployeeIntoGridView();//Long_15_06

        }
    }
}